package com.hushao.lims.weblims.service.impl;

import com.hushao.lims.weblims.domain.*;
import com.hushao.lims.weblims.repository.ClassroomRepository;
import com.hushao.lims.weblims.repository.TermRepository;
import com.hushao.lims.weblims.repository.TimetableRepository;
import com.hushao.lims.weblims.service.ClassroomService;
import com.hushao.lims.weblims.util.DateUtil;
import com.hushao.lims.weblims.util.TimetablesUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Comparator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author 930588706
 * @date 18/1/21
 */
@Service
public class ClassroomServiceImpl implements ClassroomService {

    @Autowired
    private ClassroomRepository classroomRepository;

    @Autowired
    private UserDetailsService userDetailsService;
    @Autowired
    private TimetableRepository timetableRepository;

    @Autowired
    private TermRepository termRepository;
    @Autowired
    private TimetablesUtil timetablesUtil;

    @Override
    public Page<ClassroomInfo> listClassroomByName(String classroomName, Pageable pageable) {
        classroomName="%"+classroomName+"%";
        return classroomRepository.findByClassroomNameLike(classroomName,pageable);
    }

    @Override
    public ClassroomInfo getById(Integer classroomId) {
        return classroomRepository.getOne(classroomId);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public String saveOrUpdate(ClassroomInfo classroom, String teacherNumber) throws Exception {
        TeacherInfo teacher= (TeacherInfo) userDetailsService.loadUserByUsername(teacherNumber);
        String message;
        if(classroom.getClassroomId()==null){
            classroom.setTeacherInfo(teacher);
            classroomRepository.save(classroom);
            message="添加教室成功！";
        }else{
            ClassroomInfo classroomNew=this.getById(classroom.getClassroomId());
            classroomNew.setTeacherInfo(teacher);
            classroomNew.setClassroomName(classroom.getClassroomName());
            classroomRepository.save(classroomNew);
            message="修改教室信息成功！";
        }
        return message;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void remove(Integer classroomId) throws Exception {
        classroomRepository.deleteById(classroomId);
    }

    @Override
    public Map<String, Map<String, String>> timetables(ClassroomInfo classroom) throws Exception {
        //根据当前时间判断学期
        String term= DateUtil.getTermByThisDate();
        //根据判断出的学期获取此学期在表中的记录
        TermInfo termInfo=termRepository.findByTerm(term);
        //根据实验室，学期，获取课程记录
        List<TimetableInfo> timetables=timetableRepository.findByClassroomInfoAndTermInfo(classroom,termInfo);
        //根据 周次排序
        timetables=timetables.stream().sorted(Comparator.comparing(TimetableInfo::getCycle)).collect(Collectors.toList());
        //分组：1.根据课次(2.根据星期(3.根据班级))  java8
        Map<String, Map<String, Map<ClassInfo, List<TimetableInfo>>>> map2=timetables.stream().collect(Collectors.groupingBy(TimetableInfo::getLessons,Collectors.groupingBy(TimetableInfo::getWeek,Collectors.groupingBy(TimetableInfo::getClassInfo))));
        //根据课次排序 java8
        Map<String, Map<String, Map<ClassInfo, List<TimetableInfo>>>> sortMap = new LinkedHashMap<>();
        map2.entrySet().stream().sorted(Map.Entry.<String, Map<String, Map<ClassInfo, List<TimetableInfo>>>>comparingByKey()).forEachOrdered(x -> sortMap.put(x.getKey(), x.getValue()));
        return timetablesUtil.getTimetablesByClass(sortMap);
    }


}
